<?php

namespace App\Admin\Controllers\Category;

use App\Admin\Repositories\Category\CategoryModule;
use App\Models\Category\Category;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Category\CategoryModule as ModelsCategoryModule;

class CategoryModuleController extends AdminController
{
    protected $title = '书籍模块管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CategoryModule(), function (Grid $grid) {
            $grid->model()->with(['category'])->has('category');
            $grid->column('id')->sortable();
            $grid->column('category.id', '年级Id');
            $grid->column('category.name', '年级');
            $grid->column('type', '类型')->display(function ($type) {
                return ModelsCategoryModule::$typeArray[$type];
            });
            $grid->column('name');
            $grid->column('module_type', '模块')->display(function ($module_type) {
                return ModelsCategoryModule::$moduleTypeMap[$module_type];
            });
            $grid->column('ui', '教材')->display(function ($ui) {
                return ModelsCategoryModule::$uiArray[$ui];
            });
            $grid->column('is_open', '是否开启')->switch();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('category.name', '年级')->width(2);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CategoryModule(), function (Show $show) {
            $show->field('id');
            $show->field('category_id');
            $show->field('type');
            $show->field('name');
            $show->field('module_type');
            $show->field('ui');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CategoryModule(), function (Form $form) {
            $form->select('category_id', '年级')->options(function () {
                return Category::query()->where('parent_id', 0)->orderBy('id')->pluck('name', 'id');
            });
            $form->select('type', '类型')->options(function () {
                return ModelsCategoryModule::$typeArray;
            });
            $form->hidden('name', '模块名称');
            $form->select('module_type', '模块')->options(function () {
                return ModelsCategoryModule::$moduleTypeMap;
            });
            $form->select('ui', '教材')->options(function () {
                return ModelsCategoryModule::$uiArray;
            });
            $form->switch('is_open', '是否开启');

            $form->saving(function (Form $form) {
                if ($form->module_type) {
                    $form->name = ModelsCategoryModule::$moduleTypeMap[intval($form->module_type)];
                }
            });
        });
    }
}
